package com.cskaoyan.connectionpool;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DruidUtils {


    // 数据源
    static DataSource dataSource;


    // 初始化数据源
    static {

        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("druid.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    // 获取连接
    public static Connection getConnection() {

        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }

    public static DataSource getDataSource() {
        return dataSource;
    }
}
